
#ifndef WORLDGENERATOR_H_
#define WORLDGENERATOR_H_

#include "World.h"

void generateMinable(int x, int y, int z) {
	if(getBlockId(x, y, z) == stone.blockID) {
		int i = rand()%5;
		int ore_ID = ore_coal.blockID;
		switch(i) {
		case 1:
			ore_ID = ore_iron.blockID;
			break;
		case 2:
			ore_ID = ore_gold.blockID;
			break;
		case 3:
		//	ore_ID = ore_redstone.blockID;
			break;
		case 4:
		//	ore_ID = ore_diamond.blockID;
			break;
		default:
			break;
		}
		for(int i = 0; i < 3; i++) {
			for(int j = 0; j < 3; j++) {
				for(int k = 0; k < 3; k++) {
					if(rand()%3 == 0) {
						if(getBlockId(x+i, y+j, z+k) == stone.blockID) {
							setBlockId(x+i, y+j, z+k, ore_ID);
						}
					}
				}
			}
		}
	}
}

void generateTree(int x, int y, int z) {
	if(getBlockId(x, y, z) == grass.blockID) {
		int high = 4+rand()%2;
		for(int i = 1; i <= high; i++) {
			setBlockId(x, y+i, z, wood.blockID);
		}
		for(int i = -3; i < 4; i++) {
			for(int j = -2; j < 5; j++) {
				for(int k = -3; k < 4; k++) {
					float range = sqrt(i*i + j*j + k*k);
					if(range < 2.5 && getBlockId(x+i, y+j+high, z+k) == 0) {
						setBlockId(x+i, y+j+high, z+k, leaves.blockID);
					}
				}
			}
		}
	}
}

void generateWorld() {
	for(int i = 0; i < WORLD_WIDTH/8; i++) {
		for(int j = 0; j < WORLD_HEIGHT/8; j++) {
			for(int k = 0; k < WORLD_DEPTH/8; k++) {
				int x = i*8 + rand()%8;
				int y = j*8 + rand()%8;
				int z = k*8 + rand()%8;

				generateMinable(x, y, z);

				generateTree(x, y, z);
			}
		}
	}
}

#endif /* WORLDGENERATOR_H_ */
